`timescale 1ns / 1ps
/*
Author:ChinniKrishna Kothapalli
Date  :12/09/12
Module:This is the boundary cell which does neighbour pivoting and Gaussian Elimination
Version:0.1 */



//Module to realize the boundary cell in a systolic array for Faddeev Algorithm
module boundarycellalt(
							//Inputs
	 clock,
    reset,
    count,								//Number of counts after which the boundary cell changes its behavior
    Xin, 								//Input number to be processed
							//Outputs
	 So, 									//Binary signal to indicate row exchange
    Co 									//Output value
    );
	 
							//Inputs
input wire clock;
input wire reset;
input wire [31:0] count;			//Number of counts after which the boundary cell changes its behavior
input wire signed [31:0] Xin; 	//Input number to be processed
							//Outputs
output reg  So; 						//Binary signal to indicate row exchange
output reg  [31:0] Co; 				//Output value

							//Internal Signals
reg [31:0] p=1; 						//Signal to store the internal value of thec cell
reg flag;    							//Flag which is to be affected to change the behavior of the boundary cell
reg [31:0] countvalue;				//Counter value is loaded into this.
reg state;							//To indicate the state of the division

							//Variables for division
//Inputs to divider
reg [31:0] dividend;
reg [31:0] divisor;
//Control Input of divider
reg go;									// Go indicates the divider to start division	 
//Control Output
wire done;				
//Outputs
wire[31:0] quotient;
wire[31:0] mod;
//Instantiating the division module
division32 d1(dividend,divisor,quotient,mod,go,done,clock,reset);

//Boundary Cell Begins here
always @(posedge clock)
begin
	//When the Circuit is RESET
	if(reset)
	begin
		flag<=0;									//Set the flag value to zero so that Neighbour pivoting is done first
		countvalue<=count;					//Load the counter with count value
		$display("RESET!!!");
	end
	//When not RESET
	else
	begin
		//If the counter value is less than three set flag to allow Gaussian Elimination
		if(countvalue<=3)						
		begin
			flag<=1;	
		end
		//When counter becomes zero reload it to count and set the flag back to zero.
		if(countvalue==0)						
		begin
			countvalue<=count;
			flag<=0;
		end
		//Neighbour Pivotng
		if(flag==0)
		begin
			So<=( ((Xin<0)?-Xin:Xin) > ((p<0)?-p:p) );
			if(So)
			begin
				dividend<=p;
				divisor<=Xin;
				go<=1;
				if(done)
				begin
					Co<=-1*quotient;
					p<=Xin;
					if(So==1 || So==0)
					begin
						$display("Xin:%d\tIF\tCount Value:%d\t Dividend:%d\t Divisor:%d\t Co:%d\t So:%d",Xin,countvalue,dividend,divisor,Co,So);
						countvalue<=countvalue-1;			//Decrement the counter
					end					
				end
			end
			else
			begin
				dividend<=Xin;
				divisor<=p;
				go<=1;
				if(done)
				begin
					Co<=-1*quotient;
					if(So==1 || So==0)
					begin
						$display("Xin:%d\tIF\tCount Value:%d\t Dividend:%d\t Divisor:%d\t Co:%d\t So:%d",Xin,countvalue,dividend,divisor,Co,So);
						countvalue<=countvalue-1;			//Decrement the counter
					end
				end
			end
		end
		else if(flag==1)
	begin
		if(((p<0)?-p:p)==0 )
		begin
			p<=Xin;
			countvalue<=countvalue-1;
		end
		else
		begin
			dividend<=Xin;
			divisor<=p;
			go<=1;
			if(done)
			begin
				Co<=-1*quotient;
				countvalue<=countvalue-1;
			end
		end
	end
	end
end









endmodule

